package com.nbsaas.boot.solution.rest.resource;

import com.nbsaas.boot.solution.api.apis.SolutionCatalogApi;
import com.nbsaas.boot.solution.data.entity.SolutionCatalog;
import com.nbsaas.boot.solution.api.domain.request.SolutionCatalogRequest;
import com.nbsaas.boot.solution.api.domain.response.SolutionCatalogResponse;
import com.nbsaas.boot.solution.api.domain.simple.SolutionCatalogSimple;
import com.nbsaas.boot.solution.rest.convert.SolutionCatalogSimpleConvert;
import com.nbsaas.boot.solution.rest.convert.SolutionCatalogEntityConvert;
import com.nbsaas.boot.solution.rest.convert.SolutionCatalogResponseConvert;
import com.nbsaas.boot.solution.data.repository.SolutionCatalogRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.solution.api.domain.request.SolutionCatalogSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   解决方案分类-业务接口实现
*/
@Transactional
@Service
public class SolutionCatalogResource extends BaseResource<SolutionCatalog,SolutionCatalogResponse, SolutionCatalogSimple, SolutionCatalogRequest>  implements SolutionCatalogApi {

    @Resource
    private SolutionCatalogRepository solutionCatalogRepository;

    @Override
    public JpaRepositoryImplementation<SolutionCatalog, Serializable> getJpaRepository() {
        return solutionCatalogRepository;
    }

    @Override
    public Function<SolutionCatalog, SolutionCatalogSimple> getConvertSimple() {
        return new SolutionCatalogSimpleConvert();
    }

    @Override
    public Function<SolutionCatalogRequest, SolutionCatalog> getConvertForm() {
        return new SolutionCatalogEntityConvert();
    }

    @Override
    public Function<SolutionCatalog, SolutionCatalogResponse> getConvertResponse() {
        return new SolutionCatalogResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<SolutionCatalogSimple> list(PageRequest request) {
        SolutionCatalogSimpleConvert convert=new SolutionCatalogSimpleConvert();
        if (request instanceof SolutionCatalogSearch){
             SolutionCatalogSearch searchRequest=(SolutionCatalogSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<SolutionCatalogSimple> root(SolutionCatalogSearch search) {
        ListResponse<SolutionCatalogSimple> result=new  ListResponse<>();
        List<SolutionCatalogSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


